package com.asktgapp.user.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.application.MyApplication;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.eventbus.HasNewEvent;
import com.asktgapp.eventbus.HomeEvent;
import com.asktgapp.eventbus.WebEvent;
import com.asktgapp.modulebase.common.util.LogUtil;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.modulebase.common.util.Util;
import com.sobot.chat.SobotApi;
import com.sobot.chat.api.model.Information;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;


public class ShopWebViewFragment extends BaseFragment {


    private WebView mWebView;
    private boolean reload = false;
    private RelativeLayout rlKF;
    private TextView messageNum,tvLoad;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_web, container, false);
        return super.onCreateView(inflater, rootView, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
//        getBaseActivity().setBackNavigationIcon();
        mWebView = (WebView) rootView.findViewById(R.id.baseWebView);
        messageNum = (TextView) rootView.findViewById(R.id.messageNum);
        tvLoad = (TextView) rootView.findViewById(R.id.tvLoad);
        rlKF = (RelativeLayout) rootView.findViewById(R.id.rlKF);
        rlKF.setVisibility(View.GONE);
        rlKF.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Information info = new Information();
                info.setAppkey("9d6b46705cd648b9bf1b66be4a9438bf");  //分配给App的的密钥
                //注意：uid为用户唯一标识，不能传入一样的值
                if (!Util.isEmpty(PreferencesUtil.getString(MyApplication.getInstance(), PreferencesUtil.USER_ID))) {
                    info.setUid(PreferencesUtil.getString(MyApplication.getInstance(), PreferencesUtil.USER_ID));
//用户昵称，选填
//                    info.setUname(PreferencesUtil.getString(MyApplication.getInstance(), PreferencesUtil.KEY_NAME));
//用户电话，选填
                    info.setTel(PreferencesUtil.getString(MyApplication.getInstance(), PreferencesUtil.KEY_PHONE));
                }
                SobotApi.startSobotChat(MyApplication.getInstance(), info);
            }
        });
        mWebView.loadUrl("http://shop.jg969.com:90/client/orderlist?id=" + PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
        tvLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWebView.reload();
            }
        });
        EventBus.getDefault().register(this);
    }


    @Override
    public void loadData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void hasNewMsg(WebEvent event) {
        if (SobotApi.getUnreadMsg(MyApplication.getInstance(), PreferencesUtil.getString(MyApplication.getInstance(), PreferencesUtil.USER_ID)) > 0) {
            messageNum.setVisibility(View.VISIBLE);
            if (SobotApi.getUnreadMsg(MyApplication.getInstance(), PreferencesUtil.getString(MyApplication.getInstance(), PreferencesUtil.USER_ID)) > 99) {
                messageNum.setText("99");
            } else {
                messageNum.setText(String.valueOf(SobotApi.getUnreadMsg(MyApplication.getInstance(), PreferencesUtil.getString(MyApplication.getInstance(), PreferencesUtil.USER_ID))));
            }
        } else {
            messageNum.setVisibility(View.GONE);

        }

        if (event.isGoBack()) {
//            if(mWebView.canGoBack())
//                mWebView.goBack();
//            else
//                EventBus.getDefault().post(new HasNewEvent(900));

        } else {
            String url;
            if (event.getUrl().contains("?")) {
                url = event.getUrl()
                        + "&userId=" + PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID);
            } else {
                url = event.getUrl()
                        + "?userId=" + PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID) + "&name=jiangong";
            }
            LogUtil.LogE("weburl", url);

            mWebView.loadUrl("http://shop.jg969.com:90/client/orderlist?id=" + PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (reload) {
            mWebView.reload();
        }else {
            reload = true;
        }
    }
}
